package com.codeest.geeknews.util.notify;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import com.codeest.geeknews.R;
import com.codeest.geeknews.util.okhttp.DownloadInfo;
import com.codeest.geeknews.ui.main.activity.MainActivity;

import static android.R.attr.smallIcon;

/**
 * Created by admin on 2017/7/17.
 */
public class DownloadNotifyBuilder extends NotifyBuilder {


    public final static String NOTIFICATION_ACTION_PAUSE = "NOTIFICATION_ACTION_PAUSE";
    public final static String NOTIFICATION_EXRRA = "NOTIFICATION_EXRRA";

    /**
     * @param context-
     */
    public DownloadNotifyBuilder(Context context) {
        this(context, null);
    }

    /**
     * @param context-
     * @param downloadInfo 下载相关信息
     */
    public DownloadNotifyBuilder(Context context, DownloadInfo downloadInfo) {
        super(context, downloadInfo);
        //这边设置颜色，可以给5.0及以上版本smallIcon设置背景色
        setColor(Color.RED);
        //小图标
        setSmallIcon(smallIcon);
        //大图标(这边同时设置了小图标跟大图标，在5.0及以上版本通知栏里面的样式会有所不同)
        setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
    }

    @Override
    public String ticker() {
        return "下载中..";
    }

    @Override
    public String title() {
        return "下载中";
    }

    @Override
    public String content() {
        return "下载更新";
    }

    @Override
    public int contentViewLayout() {
        return R.layout.notification_contentview;
    }

    @Override
    public PendingIntent contentIntent() {
        // 设置点击通知栏操作
        Intent in = new Intent(mContext, MainActivity.class);// 点击跳转到指定页面
        return PendingIntent.getActivity(mContext, 0, in, 0);
    }

    @Override
    protected void initOtherIntent(Object obj) {
        DownloadInfo downloadInfo = (DownloadInfo) obj;
        //可以自定义跳转界面、广播、服务
        // 设置暂停按钮的点击事件
        //----参考http://blog.csdn.net/linglongxin24/article/details/53874148
        //设置通过广播形式的PendingIntent
        //Intent pause = new Intent(mContext, OkhttpDownloadService.class);// 设置跳转到对应界面
        Intent pause = new Intent();// 设置跳转到对应界面
        pause.setAction(NOTIFICATION_ACTION_PAUSE);
        pause.putExtra(NOTIFICATION_EXRRA, downloadInfo);
        PendingIntent pauseIn = PendingIntent.getBroadcast(mContext, 1, pause, PendingIntent.FLAG_UPDATE_CURRENT);
        // 这里可以通过Bundle等传递参数
        getContentView().setOnClickPendingIntent(R.id.pause, pauseIn);
        //--------------- 简单分隔 ---------------
        // 设置取消按钮的点击事件
        Intent stop = new Intent(mContext, MainActivity.class);// 设置跳转到对应界面
        PendingIntent stopIn = PendingIntent.getActivity(mContext, 0, stop, 0);
        // 这里可以通过Bundle等传递参数
        getContentView().setOnClickPendingIntent(R.id.cancel, stopIn);

        //广播参考
        // http://blog.csdn.net/max2005/article/details/20592391
        // https://github.com/maxyou/DirPlayer/blob/master/src/com/maxproj/android/dirplayer/PlayService.java
        // Intent resultIntentLast = new Intent(this, PlayService.class);
        // PendingIntent pendingIntentLast = PendingIntent.getBroadcast(mContext, 0, resultIntentLast, PendingIntent.FLAG_UPDATE_CURRENT);
    }


}
